package com.hongpu.utils;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hongpu.enums.ResultStatusEnum;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;

/**
 * @author 张玉胜
 */
public class ResultDto implements Serializable {
    private final long serialVersionUID = 2L;
    /**
     * 状态码
     */
    private String msgCode;
    /**
     * 状态信息
     */
    private String message;
    /**
     * 返回的数据对象
     */
    private Object data;

    public ResultDto() {}

    @JsonCreator
    public ResultDto(@JsonProperty("msgCode") final String msgCode, @JsonProperty("message") final String message, @JsonProperty("data") final Object data) {
        this.msgCode = msgCode;
        this.message = message;
        this.data = data;
    }



    public static ResultDto ok() {
        ResultDto resultDto = new ResultDto(ResultStatusEnum.SUCCESS.getMsgCode(), ResultStatusEnum.SUCCESS.getMessage(), null);
        return resultDto;
    }


    public static ResultDto ok(final String msgCode, final String message) {
        ResultDto resultDto = new ResultDto(msgCode, message, null);
        return resultDto;
    }



    public static ResultDto ok(Object data) {
        ResultDto resultDto = new ResultDto(ResultStatusEnum.SUCCESS.getMsgCode(), ResultStatusEnum.SUCCESS.getMessage(), data);
        return resultDto;
    }



    public static ResultDto fail() {
        ResultDto resultDto = new ResultDto(ResultStatusEnum.FAIL.getMsgCode(), ResultStatusEnum.FAIL.getMessage(), null);
        return resultDto;
    }

    public static ResultDto fail(String message) {
        ResultDto resultDto = new ResultDto(ResultStatusEnum.FAIL.getMsgCode(), message, null);
        return resultDto;
    }


    public static ResultDto fail(String msgCode, String message) {
        ResultDto resultDto = new ResultDto(msgCode, message, null);
        return resultDto;
    }
    public static ResultDto warning(String message) {
        ResultDto resultDto;
        if(message != null){
             resultDto = new ResultDto(ResultStatusEnum.WARNING.getMsgCode(), message, null);
        }else {
             resultDto = new ResultDto(ResultStatusEnum.WARNING.getMsgCode(), ResultStatusEnum.WARNING.getMessage(), null);
        }
        return resultDto;
    }



    public static String toJsonString(Object data) throws JsonProcessingException {
        if (data == null) {
            return null;
        }
        ResultDto resultDto = new ResultDto(ResultStatusEnum.SUCCESS.getMsgCode(), ResultStatusEnum.SUCCESS.getMessage(), data);
        return new ObjectMapper().writeValueAsString(resultDto);
    }



    public static ResultDto toResultDto(String jsonString) {
        try {
            if (StringUtils.isEmpty(jsonString)) {
                return null;
            }
            JsonParser jsonParser = new ObjectMapper().createParser(jsonString);
            ResultDto resultDto = new ObjectMapper().readValue(jsonParser, ResultDto.class);
            return resultDto;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }


    @Override
    public String toString() {
        return "ResultDto{" +
                "msgCode='" + msgCode + '\'' +
                ", message='" + message + '\'' +
                ", data=" + data +
                '}';
    }

    public String getMsgCode() {
        return this.msgCode;
    }

    public void setMsgCode(final String msgCode) {
        this.msgCode = msgCode;
    }

    public String getMessage() {
        return this.message;
    }

    public void setMessage(final String message) {
        this.message = message;
    }

    public Object getData() {
        return this.data;
    }

    public void setData(final Object data) {
        this.data = data;
    }
}